<template>
  <a-modal
      title="编辑店铺"
      :width="720"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :maskClosable="false"
    :destroyOnClose="true"
      @ok="handleSubmit"
      @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
<!--        <a-form-item label="营业执照" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <SelectImage
              :defaultList="record.image ? [record.image] : []"
              v-decorator="['lincense']"
          />
        </a-form-item>-->
        <a-form-item label="营业执照名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
              v-decorator="['license', {rules: [{required: true, min: 3, message: '请输入至少3个字符'}]}]"
          />
        </a-form-item>
        <a-form-item label="店铺名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
              v-decorator="['store_name', {rules: [{required: true, min: 3, message: '请输入至少3个字符'}]}]"
          />
        </a-form-item>
        <a-form-item label="店铺登录名" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="店铺后台登录用户名">
          <a-input
             disabled="disabled" v-decorator="['user_name', {rules: [{required: true, min: 4, message: '请输入至少4个字符'}]}]"
          />
        </a-form-item>
        <a-form-item label="所属区域" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
              v-decorator="['zone_id', { rules: [{ required: true, message: '请选择1个区域' }] }]"
          >
            <a-select-option
                v-for="(item, index) in zoneList"
                :key="index"
                :value="item.zone_id"
            >{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="选择地区" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <SelectRegion
              placeholder="请选择省市区"
              v-decorator="['cascader', { rules: [{ required: true, message: '请选择省市区' }] }]"
          />
        </a-form-item>
        <a-form-item label="详细地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['detail', { rules: [{ required: true, message: '请输入详细地址' }] }]" />
        </a-form-item>
        <a-form-item label="用户密码" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="商家后台登录密码，不修改请留空。">
          <a-input
              type="password"
              v-decorator="['password', {rules: [
              { min: 6, message: '请输入至少6个字符'}
            ]}]"
          />
        </a-form-item>
        <a-form-item label="确认密码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
              type="password"
              v-decorator="['password_confirm', {rules: [
              {message: '请输入确认密码'},
              {validator: compareToFirstPassword}
            ]}]"
          />
        </a-form-item>
        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="数字越小越靠前">
          <a-input-number
              :min="0"
              v-decorator="['sort', {initialValue: 100, rules: [{required: true, message: '请输入至少1个数字'}]}]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>



import pick from 'lodash.pick'
import * as Api from '@/api/store'
import {SelectImage, SelectRegion} from '@/components'

export default {
  components: {
    SelectImage,
    SelectRegion
  },
  props: {
    // 分类列表
    zoneList: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      // 标签布局属性
      labelCol: {
        span: 7
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this)
    }
  },
  methods: {

    /**
     * 显示对话框
     */
    show () {
      this.visible = true
      this.form.resetFields()
    },

    edit (record) {
      // 显示窗口
      this.visible = true
      // 当前分类记录
      this.record = record
      // 设置默认值
      this.setFieldsValue()
    },
    // 设置默认值
    setFieldsValue () {
      const { record, form: { setFieldsValue } } = this
      // 设置表单内容
      this.$nextTick(() => {
        setFieldsValue(pick(record, ['license', 'store_name', 'user_name', 'zone_id', 'cascader', 'detail', 'sort']))
      })
    },
    /**
     * 确认按钮
     */
    handleSubmit () {
      const { form: { validateFields } } = this
      // 表单的验证
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框
     */
    handleCancel () {
      this.visible = false
      // 重置表单内容
      this.form.resetFields()
    },

    /**
     * 验证确认密码是否一致
     */
    compareToFirstPassword (rule, value, callback) {
      const { form } = this
      if (value && value !== form.getFieldValue('password')) {
        return new Error('您输入的确认密码不一致')
      }
      return true
    },

    /**
     * 提交到后端api
     */
    onFormSubmit (values) {
      this.confirmLoading = true
      Api.edit({ store_id: this.record['store_id'], form: values })
        .then(result => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally(result => {
          this.confirmLoading = false
        })
    }


  }
}
</script>
